﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using ETask.Client.Models;
using ETask.Client.Views;

namespace ETask.Client.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        public ObservableCollection<TaskItem> TaskItems { get; } = new ObservableCollection<TaskItem>();

        private double _opacity;

        public double Opacity
        {
            get => _opacity;
            set => SetProperty(ref _opacity, value);
        }

        private Visibility _taskDateVisibility;

        public Visibility TaskDateVisibility
        {
            get => _taskDateVisibility;
            set => SetProperty(ref _taskDateVisibility, value);
        }

        private DateTime? _taskDate;

        public DateTime? TaskDate
        {
            get => _taskDate;
            set
            {
                SetProperty(ref _taskDate, value);
                OnPropertyChanged(nameof(DateTitle));
                if (value.HasValue)
                {
                    TaskDateVisibility = Visibility.Collapsed;
                }
            }
        }

        public string DateTitle
        {
            get
            {
                if (!TaskDate.HasValue)
                    return "今日任务";

                // 计算选中日期与今天的差值（忽略时间，只比较日期）
                var today = DateTime.Today;
                var selectedDate = TaskDate.Value.Date;
                var daysDiff = (selectedDate - today).Days;

                return daysDiff switch
                {
                    0 => "今日任务",
                    1 => "明日任务",
                    _ => $"{selectedDate:MM月dd日}任务" 
                };
            }
        }

        public ICommand AddTaskCommand { get; }

        public ICommand DeleteTaskCommand { get; }

        public ICommand CompletedTaskCommand { get; }

        public ICommand HelpCommand { get; }

        public ICommand RegisterCommand { get; }

        public ICommand GroupCommand { get; }


        public ICommand ToggleDatePickerCommand { get; }

        public MainViewModel()
        {

            TaskDataService.Instance.Init();
            var taskList = TaskDataService.Instance.GetTaskItems(DateTime.Now);

            TaskDataService.Instance.AddEvent += Instance_AddEvent;

            TaskItems = new ObservableCollection<TaskItem>(taskList);
            AddTaskCommand = new RelayCommand(AddTask);
            DeleteTaskCommand = new RelayCommand<TaskItem>(DeleteTask, CanDeletItem);
            CompletedTaskCommand = new RelayCommand<TaskItem>(CompletedTask);
            HelpCommand = new RelayCommand(() =>
            {
                var helpWindow = new HelpWindow();
                helpWindow.ShowDialog();
            });
            RegisterCommand = new RelayCommand(() =>
            {
                var regWindow = new RegisterWindow();
                regWindow.ShowDialog();
            });
            GroupCommand = new RelayCommand(() =>
            {
                var groupWindow = new GroupWindow();
                groupWindow.ShowDialog();
            });
            ToggleDatePickerCommand = new RelayCommand(() =>
            {
                TaskDateVisibility = TaskDateVisibility == Visibility.Visible
                ? Visibility.Collapsed: Visibility.Visible;
            });
            Opacity = 70;
            TaskDateVisibility = Visibility.Collapsed;
            TaskDate = DateTime.Now;
        }

        private void Instance_AddEvent(object? sender, TaskItem e)
        {
            if (e.TaskDate.Date == DateTime.Today)
            {
                TaskItems.Add(e);
            }
        }

        private bool CanDeletItem(TaskItem item)
        {
            return item != null;
        }


        /// <summary>
        /// 添加新任务
        /// </summary>
        private void AddTask()
        {
            var addTaskWindows = new TaskWindow();
            addTaskWindows.ShowDialog();
        }


        private void DeleteTask(TaskItem item)
        {
            TaskItems.Remove(item);
            TaskDataService.Instance.RemoveTask(item);
        }

        private void CompletedTask(TaskItem item)
        {
            TaskDataService.Instance.UpdateTask(item);
        }

        public void HideDatePicker()
        {
            TaskDateVisibility = Visibility.Collapsed;
        }
    }
}
